package com.example.demo1.controller;

import com.example.demo1.pojo.Result;
import com.example.demo1.pojo.User;
import com.example.demo1.service.UserService;
import com.example.demo1.utils.JwtUtil;
import com.example.demo1.utils.Md5Util;
import com.example.demo1.utils.ThreadLocalUtil;
import jakarta.annotation.Resource;
import org.hamcrest.core.StringRegularExpression;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/register")
    public Result register(String name,String password){

        //查询用户
        //String name = username;
        User u=userService.findByName(name);
        if(u==null){
            userService.register(name,password);
            return Result.success();
        }else{
            return Result.error("用户名已被占用");
        }
        //注册
    }

    @PostMapping("/login")
    public Result<String> login(String name,String password){
        User loginuser = userService.findByName(name);
        if(loginuser==null){
            return Result.error("用户名错误");
        }

        if(Md5Util.passwordIsTrue(password,loginuser.getPassword())){

            Map<String,Object> claims=new HashMap<>();
            claims.put("id",loginuser.getId());
            claims.put("name",loginuser.getName());
            String genToken = JwtUtil.genToken(claims);
            ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
            operations.set(genToken,genToken,1, TimeUnit.DAYS);
            return Result.success(genToken);
        }

        return Result.error("密码错误");
    }

    @GetMapping("/userInfo")
    public Result<User> userInfo(){
        Map<String ,Object> map = ThreadLocalUtil.get();
        String name = (String) map.get("name");
        User user = userService.findByName(name);
        return Result.success(user);
    }


    @PutMapping("/updateuser")
    public Result update(@RequestBody @Validated User user){
        userService.update(user);
        return Result.success();
    }


    @PostMapping("/avatar")
    public Result<String> upload(@RequestBody @RequestParam(value = "file", required = false) MultipartFile file, Integer id){
        //System.out.println(file.getName());
        if(file==null){
            return Result.success();
        }
        String pathFile = userService.upload(file, id);
        System.out.println(pathFile);
        return Result.success(pathFile);
    }




    @PatchMapping("/updatePwd")
    public Result updatePwd(@RequestBody Map<String,String> params,@RequestHeader("Authorization") String token){
        String oldPwd = params.get("old_pwd");
        String newPwd = params.get("new_pwd");
        String rePwd = params.get("re_pwd");

        System.out.println(oldPwd+" "+newPwd+" "+rePwd);
        if(!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)){
            return Result.error("缺少不要参数");
        }

        Map<String,Object> user = ThreadLocalUtil.get();
        String name = String.valueOf(user.get("name"));
        User loginUser = userService.findByName(name);
        if(!Md5Util.passwordIsTrue(oldPwd,loginUser.getPassword())){
            return Result.error("原密码填写不正确");
        }
        if(!rePwd.equals(newPwd)){
            return Result.error("两次输入密码不一致");
        }
        userService.updatePwd(newPwd);
        //删除redis中原有的token
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        operations.getOperations().delete(token);
        return Result.success();
    }
}
